package com.infra.micro.id.model.enums;

import com.infra.common.enums.BaseEnum;
import com.infra.common.resp.KeyValue;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public enum IdLifecycleStateEnum implements BaseEnum {

    DEFAULT( 0,"默认状态"),
    INIT( 1,"初始化状态"),
    START( 2,"启动状态"),
    STOP( 3,"停止状态");
    /**
     * 状态码
     */
    private int code;

    /**
     * 状态码描述
     */
    private String message;

    IdLifecycleStateEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 通过码获取枚举项
     *
     * @param code
     * @return
     */
    public static IdLifecycleStateEnum getByCode(int code) {
        return Arrays.stream(IdLifecycleStateEnum.values())
                .filter(item -> item.getCode() == code)
                .findFirst()
                .orElse(IdLifecycleStateEnum.DEFAULT);
    }

    /**
     * 枚举项值转为List
     * e.g. ChecksumEnum.toList();
     *
     * @return
     */
    public static List<KeyValue> toList() {
        return Arrays.stream(IdLifecycleStateEnum.values())
                .map(item -> new KeyValue(String.valueOf(item.getCode()), item.getMessage()))
                .collect(Collectors.toList());
    }

}
